//
//  NSString+ContentVerify.m
//  SPKit
//
//  Created by 李双鹏 on 15/10/5.
//  Copyright (c) 2015年 SP. All rights reserved.
//

#import "NSString+ContentVerify.h"

@implementation NSString (ContentVerify)
// 验证手机号
-(BOOL)isValidPhoneNumber{
    NSString *temp = [self stringByReplacingOccurrencesOfString:@"+" withString:@""];
    NSString *regex = @"^(0|86|17951|17950|17911)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
//    @"^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    NSPredicate *pdc = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pdc evaluateWithObject:temp];
}

// 验证身份证号
-(BOOL)isValidIDCard{
    if (self.length == 15) {
        return [self isIDCard1];
    }else if (self.length == 18){
        return [self isIDCard2];
    }else
        return NO;
}
-(BOOL)isIDCard1{
    NSString *regex = @"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
    NSPredicate *pdc = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pdc evaluateWithObject:self];
}
-(BOOL)isIDCard2{
    NSLog(@"sting2");
    NSString *regex = @"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])(\\d{3}([0-9]|X|x))$";
    NSPredicate *pdc = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pdc evaluateWithObject:self];
}

// Is Valid Email
- (BOOL)isValidEmail
{
    NSString *regex = @"[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    NSPredicate *pdc = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pdc evaluateWithObject:self];
}

- (BOOL)isValidPostCode{
    NSString *regex = @"[1-9]\\d{5}(?!\\d)";
    NSPredicate *pdc = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pdc evaluateWithObject:self];
}
// remove white spaces from String
- (NSString *)removeWhiteSpaces
{
    NSString *trimmedString = [self stringByTrimmingCharactersInSet:
                               [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return trimmedString;
}
// Checking if String is Empty
-(BOOL)isBlank
{
    return [[self removeWhiteSpaces] isEqualToString:@""];
}
// Counts number of Words in String
- (NSUInteger)countNumberOfWords
{
    NSScanner *scanner = [NSScanner scannerWithString:self];
    NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    
    NSUInteger count = 0;
    while ([scanner scanUpToCharactersFromSet: whiteSpace  intoString: nil]) {
        count++;
    }
    
    return count;
}
// If my string starts with given string
- (BOOL)isBeginsWith:(NSString *)string
{
    return [self hasPrefix:string];
}
// If my string ends with given string
- (BOOL)isEndsWith:(NSString *)string
{
    return [self hasSuffix:string];
}
// Replace particular characters in my string with new character
- (NSString *)replaceString:(NSString *)olderChar withString:(NSString *)newerChar
{
    return  [self stringByReplacingOccurrencesOfString:olderChar withString:newerChar];
}
// Remove particular sub string from main string
-(NSString *)removeSubString:(NSString *)subString
{
    if ([self containsString:subString])
    {
//        NSRange range = [self rangeOfString:subString];
//        return  [self stringByReplacingCharactersInRange:range withString:@""];
        return [self stringByReplacingOccurrencesOfString:subString withString:@""];
    }
    return self;
}
// If my string contains ony letters
- (BOOL)isContainsOnlyLetters
{
    NSCharacterSet *letterCharacterset = [[NSCharacterSet letterCharacterSet] invertedSet];
    return ([self rangeOfCharacterFromSet:letterCharacterset].location == NSNotFound);
}
// If my string contains only numbers
- (BOOL)isContainsOnlyNumbers
{
    NSCharacterSet *numbersCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    return ([self rangeOfCharacterFromSet:numbersCharacterSet].location == NSNotFound);
}
// If my string contains letters and numbers
- (BOOL)isContainsOnlyNumbersAndLetters
{
    NSCharacterSet *numAndLetterCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    return ([self rangeOfCharacterFromSet:numAndLetterCharSet].location == NSNotFound);
}
// If my string is available in particular array
- (BOOL)isContainedInArray:(NSArray*)array
{
    for(NSString *string in array) {
        if([self isEqualToString:string]) {
            return YES;
        }
    }
    return NO;
}

- (BOOL)containsString:(NSString *)subString
{
    return [self rangeOfString:subString options:NSLiteralSearch].location != NSNotFound;
}
@end
